//
//  MASConstraintMaker_yjw.m
//  Masonry
//
//  Created by Jonas Budelmann on 20/07/13.
//  Copyright (c) 2013 cloudling. All rights reserved.
//

#import "MASConstraintMaker_yjw.h"
#import "MASViewConstraint_yjw.h"
#import "MASCompositeConstraint_yjw.h"
#import "MASConstraint_yjw+Private.h"
#import "MASViewAttribute_yjw.h"
#import "View+MASAdditions_yjw.h"

@interface MASConstraintMaker_yjw () <MASConstraint_yjwDelegate>

@property (nonatomic, weak) MAS_YJW_VIEW *view;
@property (nonatomic, strong) NSMutableArray *constraints;

@end

@implementation MASConstraintMaker_yjw

- (id)initWithView:(MAS_YJW_VIEW *)view {
    self = [super init];
    if (!self) return nil;
    
    self.view = view;
    self.constraints = NSMutableArray.new;
    
    return self;
}

- (NSArray *)install {
    if (self.removeExisting) {
        NSArray *installedConstraints = [MASViewConstraint_yjw installedConstraintsForView:self.view];
        for (MASConstraint_yjw *constraint in installedConstraints) {
            [constraint uninstall];
        }
    }
    NSArray *constraints = self.constraints.copy;
    for (MASConstraint_yjw *constraint in constraints) {
        constraint.updateExisting = self.updateExisting;
        [constraint install];
    }
    [self.constraints removeAllObjects];
    return constraints;
}

#pragma mark - MASConstraint_yjwDelegate

- (void)constraint:(MASConstraint_yjw *)constraint shouldBeReplacedWithConstraint:(MASConstraint_yjw *)replacementConstraint {
    NSUInteger index = [self.constraints indexOfObject:constraint];
    NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
    [self.constraints replaceObjectAtIndex:index withObject:replacementConstraint];
}

- (MASConstraint_yjw *)constraint:(MASConstraint_yjw *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    MASViewAttribute_yjw *viewAttribute = [[MASViewAttribute_yjw alloc] initWithView:self.view layoutAttribute:layoutAttribute];
    MASViewConstraint_yjw *newConstraint = [[MASViewConstraint_yjw alloc] initWithFirstViewAttribute:viewAttribute];
    if ([constraint isKindOfClass:MASViewConstraint_yjw.class]) {
        //replace with composite constraint
        NSArray *children = @[constraint, newConstraint];
        MASCompositeConstraint_yjw *compositeConstraint = [[MASCompositeConstraint_yjw alloc] initWithChildren:children];
        compositeConstraint.delegate = self;
        [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
        return compositeConstraint;
    }
    if (!constraint) {
        newConstraint.delegate = self;
        [self.constraints addObject:newConstraint];
    }
    return newConstraint;
}

- (MASConstraint_yjw *)addConstraintWithAttributes:(MASAttribute_yjw)attrs {
    __unused MASAttribute_yjw anyAttribute = (MASAttribute_yjwLeft | MASAttribute_yjwRight | MASAttribute_yjwTop | MASAttribute_yjwBottom | MASAttribute_yjwLeading
                                          | MASAttribute_yjwTrailing | MASAttribute_yjwWidth | MASAttribute_yjwHeight | MASAttribute_yjwCenterX
                                          | MASAttribute_yjwCenterY | MASAttribute_yjwBaseline
                                          | MASAttribute_yjwFirstBaseline | MASAttribute_yjwLastBaseline
#if TARGET_OS_IPHONE || TARGET_OS_TV
                                          | MASAttribute_yjwLeftMargin | MASAttribute_yjwRightMargin | MASAttribute_yjwTopMargin | MASAttribute_yjwBottomMargin
                                          | MASAttribute_yjwLeadingMargin | MASAttribute_yjwTrailingMargin | MASAttribute_yjwCenterXWithinMargins
                                          | MASAttribute_yjwCenterYWithinMargins
#endif
                                          );
    
    NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)");
    
    NSMutableArray *attributes = [NSMutableArray array];
    
    if (attrs & MASAttribute_yjwLeft) [attributes addObject:self.view.mas_left];
    if (attrs & MASAttribute_yjwRight) [attributes addObject:self.view.mas_right];
    if (attrs & MASAttribute_yjwTop) [attributes addObject:self.view.mas_top];
    if (attrs & MASAttribute_yjwBottom) [attributes addObject:self.view.mas_bottom];
    if (attrs & MASAttribute_yjwLeading) [attributes addObject:self.view.mas_leading];
    if (attrs & MASAttribute_yjwTrailing) [attributes addObject:self.view.mas_trailing];
    if (attrs & MASAttribute_yjwWidth) [attributes addObject:self.view.mas_width];
    if (attrs & MASAttribute_yjwHeight) [attributes addObject:self.view.mas_height];
    if (attrs & MASAttribute_yjwCenterX) [attributes addObject:self.view.mas_centerX];
    if (attrs & MASAttribute_yjwCenterY) [attributes addObject:self.view.mas_centerY];
    if (attrs & MASAttribute_yjwBaseline) [attributes addObject:self.view.mas_baseline];
    if (attrs & MASAttribute_yjwFirstBaseline) [attributes addObject:self.view.mas_firstBaseline];
    if (attrs & MASAttribute_yjwLastBaseline) [attributes addObject:self.view.mas_lastBaseline];
    
#if TARGET_OS_IPHONE || TARGET_OS_TV
    
    if (attrs & MASAttribute_yjwLeftMargin) [attributes addObject:self.view.mas_leftMargin];
    if (attrs & MASAttribute_yjwRightMargin) [attributes addObject:self.view.mas_rightMargin];
    if (attrs & MASAttribute_yjwTopMargin) [attributes addObject:self.view.mas_topMargin];
    if (attrs & MASAttribute_yjwBottomMargin) [attributes addObject:self.view.mas_bottomMargin];
    if (attrs & MASAttribute_yjwLeadingMargin) [attributes addObject:self.view.mas_leadingMargin];
    if (attrs & MASAttribute_yjwTrailingMargin) [attributes addObject:self.view.mas_trailingMargin];
    if (attrs & MASAttribute_yjwCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins];
    if (attrs & MASAttribute_yjwCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins];
    
#endif
    
    NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count];
    
    for (MASViewAttribute_yjw *a in attributes) {
        [children addObject:[[MASViewConstraint_yjw alloc] initWithFirstViewAttribute:a]];
    }
    
    MASCompositeConstraint_yjw *constraint = [[MASCompositeConstraint_yjw alloc] initWithChildren:children];
    constraint.delegate = self;
    [self.constraints addObject:constraint];
    return constraint;
}

#pragma mark - standard Attributes

- (MASConstraint_yjw *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute];
}

- (MASConstraint_yjw *)left {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
}

- (MASConstraint_yjw *)top {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop];
}

- (MASConstraint_yjw *)right {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight];
}

- (MASConstraint_yjw *)bottom {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom];
}

- (MASConstraint_yjw *)leading {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading];
}

- (MASConstraint_yjw *)trailing {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing];
}

- (MASConstraint_yjw *)width {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth];
}

- (MASConstraint_yjw *)height {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight];
}

- (MASConstraint_yjw *)centerX {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX];
}

- (MASConstraint_yjw *)centerY {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY];
}

- (MASConstraint_yjw *)baseline {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline];
}

- (MASConstraint_yjw *(^)(MASAttribute_yjw))attributes {
    return ^(MASAttribute_yjw attrs){
        return [self addConstraintWithAttributes:attrs];
    };
}

- (MASConstraint_yjw *)firstBaseline {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline];
}

- (MASConstraint_yjw *)lastBaseline {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline];
}

#if TARGET_OS_IPHONE || TARGET_OS_TV

- (MASConstraint_yjw *)leftMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin];
}

- (MASConstraint_yjw *)rightMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin];
}

- (MASConstraint_yjw *)topMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin];
}

- (MASConstraint_yjw *)bottomMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin];
}

- (MASConstraint_yjw *)leadingMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin];
}

- (MASConstraint_yjw *)trailingMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin];
}

- (MASConstraint_yjw *)centerXWithinMargins {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins];
}

- (MASConstraint_yjw *)centerYWithinMargins {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins];
}

#endif


#pragma mark - composite Attributes

- (MASConstraint_yjw *)edges {
    return [self addConstraintWithAttributes:MASAttribute_yjwTop | MASAttribute_yjwLeft | MASAttribute_yjwRight | MASAttribute_yjwBottom];
}

- (MASConstraint_yjw *)size {
    return [self addConstraintWithAttributes:MASAttribute_yjwWidth | MASAttribute_yjwHeight];
}

- (MASConstraint_yjw *)center {
    return [self addConstraintWithAttributes:MASAttribute_yjwCenterX | MASAttribute_yjwCenterY];
}

#pragma mark - grouping

- (MASConstraint_yjw *(^)(dispatch_block_t group))group {
    return ^id(dispatch_block_t group) {
        NSInteger previousCount = self.constraints.count;
        group();

        NSArray *children = [self.constraints subarrayWithRange:NSMakeRange(previousCount, self.constraints.count - previousCount)];
        MASCompositeConstraint_yjw *constraint = [[MASCompositeConstraint_yjw alloc] initWithChildren:children];
        constraint.delegate = self;
        return constraint;
    };
}

@end
